package com.example.carrierapp.ui.adapter;

import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.carrierapp.bean.waybill.WaybillListBean;
import com.example.carrierapp.ui.view.recycleview.XRecyclerView;
import com.example.carrierapp.util.DateUtils;
import com.example.lb.fingerdemo.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class WaybillArriveYesAdatpter extends XRecyclerView.Adapter<WaybillArriveYesAdatpter.ViewHolder> {
    private List<WaybillListBean.WaybillListInfo> mList = new ArrayList();
    private int mClickIndex;
    public WaybillArriveYesAdatpter() {
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_waybill_arriveyes, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        WaybillListBean.WaybillListInfo info = mList.get(position);
        holder.bindData(info,position);
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public void addDataNotClear(List<WaybillListBean.WaybillListInfo> list) {
        if (list != null && list.size() > 0) {
            mList.addAll(list);
            notifyDataSetChanged();
        }
    }

    public void addData(List<WaybillListBean.WaybillListInfo> list) {
        if (list != null && list.size() > 0) {
            mList.clear();
            mList.addAll(list);
            notifyDataSetChanged();
        }
    }

    public void clear() {
        mList.clear();
        notifyDataSetChanged();
    }

    public void updataItemData(WaybillListBean.WaybillListInfo bean) {
        mList.remove(mClickIndex);
        notifyItemRemoved(mClickIndex);

        mList.add(mClickIndex, bean);
        notifyItemInserted(mClickIndex);

        notifyDataSetChanged();
    }

    public class ViewHolder extends XRecyclerView.ViewHolder {
        @BindView(R.id.tv_waybill_address_start)
        TextView mTvAddressStart;
        @BindView(R.id.tv_waybill_address_end)
        TextView mTvAddressEnd;
        @BindView(R.id.tv_waybill_home_code)
        TextView mTvCode;
        @BindView(R.id.tv_waybill_in_car)
        TextView mTvTimeIn;
        @BindView(R.id.tv_waybill_out_car)
        TextView mTvTimeOut;
        @BindView(R.id.tv_waybill_goods)
        TextView mTvGoods;
        @BindView(R.id.tv_waybill_person)
        TextView mTvPerson;

        @BindView(R.id.tv_waybill_detail)
        TextView mTvSeeDetail;
        @BindView(R.id.iv_waybill_verify)
        ImageView mIvVerify;
        @BindView(R.id.tv_waybill_modify)
        TextView mTvModifyVolume;
        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public void bindData(WaybillListBean.WaybillListInfo info, int position) {
            mTvAddressStart.setText(info.pickCityName);
            mTvAddressEnd.setText(info.takeCityName);
            mTvTimeIn.setText("装 " + (TextUtils.isEmpty(info.loadingTime)?"暂无装货时间":DateUtils.creatYearMinuteTime(info.loadingTime)));
            mTvTimeOut.setText("卸 " + (TextUtils.isEmpty(info.signTime)?"暂无卸货时间":DateUtils.creatYearMinuteTime(info.signTime)));
            mTvCode.setText(info.code);
            mTvGoods.setText(info.proName + " 总" + info.dispatchNum + "吨");
            mTvPerson.setText(info.driverName);
            //装车信息审核状态0、未审核 1、审核通过 2、审核未通过 3 重新上传
            switch (info.isLoadingActive) {
                case 0:
                case 2:
                    mIvVerify.setVisibility(View.VISIBLE);
                    mIvVerify.setImageResource(R.mipmap.icon_verify_un);
                    break;
                case 1:
                    mIvVerify.setVisibility(View.VISIBLE);
                    mIvVerify.setImageResource(R.mipmap.icon_verify_yes);
                    break;
            }
            mTvModifyVolume.setVisibility(info.payState<=0||info.payState==5?View.VISIBLE:View.GONE);
            mTvSeeDetail.setOnClickListener(v -> {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onDetailClick(info);
                }
            });
            mTvModifyVolume.setOnClickListener(v -> {
                if(mOnItemClickListener!=null)
                {
                    mOnItemClickListener.onModifyClick(info);
                    mClickIndex = position;
                }
            });
        }
    }

    public interface OnBtnClickListener {
        void onDetailClick(WaybillListBean.WaybillListInfo info);

        void onModifyClick(WaybillListBean.WaybillListInfo info);
    }

    private OnBtnClickListener mOnItemClickListener;

    public void setOnBtnClickListener(OnBtnClickListener listener) {
        mOnItemClickListener = listener;
    }
}
